using System;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SmartHotel.FacilityManagementWeb.Models;
using Swashbuckle.AspNetCore.Swagger;

namespace SmartHotel.FacilityManagementWeb
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });

	        services.AddSwaggerGen(c =>
	        {
		        //The generated Swagger JSON file will have these properties.
		        c.SwaggerDoc("v1", new Info
		        {
			        Title = "SmartHotel.FacilityManagementWeb",
			        Version = "v1",
		        });
		        //Locate the XML file being generated by ASP.NET...
		        var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
		        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
		        //... and tell Swagger to use those XML comments.
		        c.IncludeXmlComments(xmlPath);
	        });
	        services.Configure<AdalConfig>(Configuration.GetSection("adalConfig"));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
	            app.UseSwagger();
	            app.UseSwaggerUI(c =>
	            {
		            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger Endpoint");
	            });
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });

            app.UseSpa(spa =>
            {
                // To learn more about options for serving an Angular SPA from ASP.NET Core,
                // see https://go.microsoft.com/fwlink/?linkid=864501

                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });
        }
    }
}
